# -*- coding: utf-8 -*-
# $Id: batchesmodel.py 15 2009-02-14 22:48:00Z gkuznets $

# Copyright 2008, 2009 Grigoriy Kuznetsov (gkuznets@yandex.ru)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4.QtCore import QAbstractListModel, Qt, QVariant, QString, SIGNAL
from PyQt4.QtGui import QBrush
from batchicon import renderBatchIcon

class GroupModel(QAbstractListModel):
    """Модель для списка пачек находящихся на проверке в группе"""
    
    def __init__(self, group, parent=None):
        QAbstractListModel.__init__(self, parent)
        self.__group = group
        
    def data(self, mindex, role):
        if not mindex.isValid():
            return QVariant()
        
        if role == Qt.DisplayRole:
            return QVariant(QString(self.__group.blist[mindex.row()].name()))
        
        if role == Qt.DecorationRole:
            return QVariant(renderBatchIcon(self.__group.blist[mindex.row()]))
        
        if role == Qt.BackgroundRole:
            if self.__group.blist[mindex.row()].lazy:
                return QVariant(QBrush(Qt.red))
            else:
                return QVariant(QBrush(Qt.white))
        
        return QVariant()
    
    def flags(self, model_index):
        if model_index.isValid():
                return Qt.ItemIsEnabled
        else:
            return Qt.NoItemFlags
    
    def rowCount(self, parent_index=None):
        return len(self.__group.blist)
    
    def onGroupChanged(self, gno):
        if self.__group.index == gno:
            self.emit(SIGNAL("modelReset()"))